package com.topisv.tms.service.report;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import com.topisv.tms.mybatis.Pagination;
import com.topisv.tms.mybatis.service.CommonService;
import com.topisv.tms.tools.ExcelStUtil;
import com.topisv.tms.vo.report.KpiReportVo;

/**
 * KPI出库订单报表
 *
 * @author yuansheng
 * @version 1.0.0
 */
@Service
public class KpiOutOrderService extends CommonService {

    public Pagination getPageKpiReceive(KpiReportVo vo) throws Exception {
        Pagination page = myBatisPage("kpiOutOrderMapper.getCount", "kpiOutOrderMapper.getList", vo);
        return page;
    }

    public List<Object> getOPName() {
        return sqlMapDao.getSqlSession().selectList("kpiOutOrderMapper.getOPName");
    }

    public void exportExcel(KpiReportVo vo, HttpServletResponse response) throws Exception {
        OutputStream output = response.getOutputStream();
        List rglist = sqlMapDao.selectList("kpiOutOrderMapper.getList", vo);
        List<Object[]> list = new ArrayList<Object[]>();
        for (Object object : rglist) {
            LinkedHashMap<String, String> map = (LinkedHashMap<String, String>) object;
            Object[] obj = new Object[19];
            obj[0] = map.get("id");
            obj[1] = map.get("出库单号");
            obj[2] = map.get("仓库");
            obj[3] = map.get("商家名称");
            obj[4] = map.get("接单时间");
            obj[5] = map.get("创建波次时间");
            obj[6] = map.get("创建波次人");
            obj[7] = map.get("拣货完成时间");
            obj[8] = map.get("拣货人");
            obj[9] = map.get("复核完成时间");
            obj[10] = map.get("复核人");
            obj[11] = map.get("称重时间");
            obj[12] = map.get("称重操作人");
            obj[13] = map.get("创建日期");
            list.add(obj);
        }

        response.setContentType("application/octet-stream");// 设置后会呼出下载界面
        // 头信息
        String[] titles = { "数据库序号", "出库单号", "仓库", "商家名称", "接单时间", "创建波次时间", "创建波次人", "拣货完成时间", "拣货人", "复核完成时间", "复核人", "称重时间", "称重操作人", "创建日期" };
        // 文件名
        String fileName = new String(("KPI收货报表.xls").getBytes("GBK"), "ISO8859-1");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        String pageTitle = "KPI收货报表";
        Date now = new Date();
        ExcelStUtil.export(output, DateFormatUtils.format(now, "yyyyMMddhhmmss"), pageTitle, titles, list);
        output.close();
    }
}
